package com.atomdayl.domain.therecipe.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.Nullable;

import com.atomdayl.domain.therecipe.RecipeApplication;
import com.atomdayl.domain.therecipe.dummy.UserModel;

/**
 * Created by David on 2016/6/5.
 */
public class PreferencesUtils {

    public static final String CLIENT_LOGIN = "client_login";
    public static final String AB_FLAG = "ab_flag";
    public static final String AB_TYPE = "ab_type";
    public static final String USER_INFO = "user_info";
    public static final String USER_USERNAME = "user_username";
    public static final String USER_PASSWORD = "user_password";
    public static final String USER_NAME = "user_name";

    public static int getInt(Context context, String type, String key, int defaultValue) {
        SharedPreferences preference = context.getSharedPreferences(type, Context.MODE_PRIVATE);
        return preference.getInt(key, defaultValue);
    }

    /**
     * 保存密码
     * @param context
     * @param userModel
     */
    public static void saveUser(Context context, @Nullable UserModel userModel) {
        SharedPreferences preferences = context.getSharedPreferences(USER_INFO, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = preferences.edit();
        edit.putString(USER_USERNAME, userModel.getUsername());
        edit.putString(USER_NAME, userModel.getName());
        edit.putString(USER_PASSWORD, userModel.getPassword());
        edit.commit();
    }

    /**
     * 获取密码
     * @param context
     * @return
     */
    public static UserModel getUser(Context context) {
        SharedPreferences preferences = context.getSharedPreferences(USER_INFO, Context.MODE_PRIVATE);
        String name = preferences.getString(USER_NAME, "");
        String username = preferences.getString(USER_USERNAME, "");
        String password = preferences.getString(USER_PASSWORD, "");
        UserModel userModel = new UserModel();
        userModel.setName(name);
        userModel.setUsername(username);
        userModel.setPassword(password);
        return userModel;
    }

}
